#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>


void Swap(int* p1, int* p2)
{
	int tem = *p1;
	*p1 = *p2;
	*p2 = tem;
}

int GetMinNum(int* a, int begin, int end)
{
	int min = (begin + end) / 2;
	//a[begin] > a[min]
	if (a[begin] > a[min])
	{
		if (a[min] > a[end])
		{
			return min;
		}
		else if(a[begin] > a[end])
		{
			return end;
		}
		else
		{
			return begin;
		}
	}
	//a[begin] <= a[min]
	else
	{
		if (a[min] < a[end])
		{
			return min;
		}
		else if (a[begin] < a[end])
		{
			return end;
		}
		else
		{
			return begin;
		}
	}
}

int Partion1(int* a, int left, int right)
{
	int mini = GetMinNum(a, left, right);
	Swap(&a[mini], &a[left]);
	int keyi = left;
	int prev = left;
	int cur = prev + 1;
	while (cur <= right)
	{
		if (a[cur] < a[keyi] && prev++ != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;
	return keyi;
}

void PrinArray(int* a, int len)
{
	for (int i = 0; i < len; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void QuickSort(int* a, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int keyi = Partion1(a, begin, end);
	// [begin,keyi - 1] keyi [keyi + 1,end]
	QuickSort(a, begin, keyi - 1);
	QuickSort(a, keyi + 1, end);
}

void test1()
{
	int a[] = {1,3,5,6,7,9,8,89 };
	int len = sizeof(a) / sizeof(int);
	QuickSort(a, 0, len - 1);
	PrinArray(a, len);
}

int main()
{
	test1();

	return 0;
}